package com.strange.mdl.module.mdl.controller.admin.incense;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.strange.mdl.framework.common.pojo.PageParam;
import com.strange.mdl.framework.common.pojo.PageResult;
import com.strange.mdl.framework.common.pojo.CommonResult;
import com.strange.mdl.framework.common.util.object.BeanUtils;
import static com.strange.mdl.framework.common.pojo.CommonResult.success;

import com.strange.mdl.framework.excel.core.util.ExcelUtils;

import com.strange.mdl.framework.apilog.core.annotation.ApiAccessLog;
import static com.strange.mdl.framework.apilog.core.enums.OperateTypeEnum.*;

import com.strange.mdl.module.mdl.controller.admin.incense.vo.*;
import com.strange.mdl.module.mdl.dal.dataobject.incense.IncenseDO;
import com.strange.mdl.module.mdl.service.incense.IncenseService;

@Tag(name = "管理后台 - 供香记录")
@RestController
@RequestMapping("/mdl/incense")
@Validated
public class IncenseController {

    @Resource
    private IncenseService incenseService;

    @PostMapping("/create")
    @Operation(summary = "创建供香记录")
    @PreAuthorize("@ss.hasPermission('mdl:incense:create')")
    public CommonResult<Long> createIncense(@Valid @RequestBody IncenseSaveReqVO createReqVO) {
        return success(incenseService.createIncense(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新供香记录")
    @PreAuthorize("@ss.hasPermission('mdl:incense:update')")
    public CommonResult<Boolean> updateIncense(@Valid @RequestBody IncenseSaveReqVO updateReqVO) {
        incenseService.updateIncense(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除供香记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mdl:incense:delete')")
    public CommonResult<Boolean> deleteIncense(@RequestParam("id") Long id) {
        incenseService.deleteIncense(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除供香记录")
                @PreAuthorize("@ss.hasPermission('mdl:incense:delete')")
    public CommonResult<Boolean> deleteIncenseList(@RequestParam("ids") List<Long> ids) {
        incenseService.deleteIncenseListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得供香记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mdl:incense:query')")
    public CommonResult<IncenseRespVO> getIncense(@RequestParam("id") Long id) {
        IncenseDO incense = incenseService.getIncense(id);
        return success(BeanUtils.toBean(incense, IncenseRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得供香记录分页")
    @PreAuthorize("@ss.hasPermission('mdl:incense:query')")
    public CommonResult<PageResult<IncenseRespVO>> getIncensePage(@Valid IncensePageReqVO pageReqVO) {
        PageResult<IncenseDO> pageResult = incenseService.getIncensePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IncenseRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出供香记录 Excel")
    @PreAuthorize("@ss.hasPermission('mdl:incense:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIncenseExcel(@Valid IncensePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IncenseDO> list = incenseService.getIncensePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "供香记录.xls", "数据", IncenseRespVO.class,
                        BeanUtils.toBean(list, IncenseRespVO.class));
    }

}